package cn.techaction.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import cn.techaction.dao.ActionOrderItemDao;
import cn.techaction.pojo.ActionorderItem;
@Repository
public class ActionOrderItemDaoImpl implements ActionOrderItemDao {
	@Resource
	private QueryRunner queryRunner;
	
	private String alias="id,uid,order_id as orderId,goods_id as goodsId,"
			+ "goods_name as goodsNamee,icon_url as iconUrl,price,quantity,total_price as totalPrice,created,updated";

	@Override
	public List<ActionorderItem> getItemsByOrderNo(Long orderNo) {
		// TODO Auto-generated method stub
		String sql="select "+this.alias+" from list_item where order_id = ?";
		try {
			return queryRunner.query(sql, new BeanListHandler<ActionorderItem>(ActionorderItem.class),orderNo);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}

	@Override
	public int[] batchInsert(List<ActionorderItem> orderItems) {
		// TODO Auto-generated method stub
		String sql ="insert into list_item(uid,order_id,goods_id,"
				+ "goods_name,icon_url,price,quantity,total_price,created,updated)"
				+ " values(?,?,?,?,?,?,?,?,?,?)";
		Object[][] params=new Object[orderItems.size()][];
		for (int i = 0; i < orderItems.size(); i++) {
			ActionorderItem item=orderItems.get(i);
			params[i]=new Object[] {
					item.getUid(),item.getOrderNo(),item.getGoodsId(),item.getGoodsName(),item.getIconUrl(),item.getPrice(),
					item.getQuantity(),item.getTotalPrice(),item.getCreated(),item.getUpdated()
			};
			
		}
		try {
			return queryRunner.batch(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}

}
